﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.FIM;
using KPIS.GERP.Models.SYS;
using WebMatrix.WebData;

namespace KPIS.GERP.WEB.GFMS.Controllers
{
    public class ChequeBookController : Controller
    {
        #region ChequeBook
        public ActionResult Index()
        {
            var service = new  ChequeBookService.ChequeBookServiceClient();
            return View(service.SelectChequeBook(new Models.FIM.ChequeBook { RecordStatus = "!D" }, new[] { "MasBankAccount" }));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Create(ChequeBook model)
        {
            try
            {
                var service = new ChequeBookService.ChequeBookServiceClient();
                var countCode = service.SelectChequeBook(new ChequeBook() { Code = model.Code, RecordStatus = "!D" }, null).Length;

                if (countCode > 0)
                {
                    ModelState.AddModelError("", "!เกิดข้อผิดพลาด เลขที่สมุดเช็คซ้ำ");
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        model.ChequeBookStatus = "1";

                        var id = service.InsertChequeBook(model, WebSecurity.CurrentUserId);
                        return RedirectToAction("Details", new { id = id });

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var service = new ChequeBookService.ChequeBookServiceClient();
            var model = service.SelectChequeBook(new Models.FIM.ChequeBook { ChequeBookId = id }, null).FirstOrDefault();

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, ChequeBook model)
        {
            try
            {
                var service = new ChequeBookService.ChequeBookServiceClient();
                var oldCode = service.SelectChequeBook(new ChequeBook() { ChequeBookId = model.ChequeBookId }, null).FirstOrDefault();
                var oldCBookId = oldCode.Code;

                if (oldCBookId == model.Code)
                {
                    if (ModelState.IsValid)
                    {
                        model.ChequeBookStatus = "1";
                        service.UpdateChequeBook(model, WebSecurity.CurrentUserId);

                        return RedirectToAction("Details", new { id = id });
                    }
                   
                }
                else
                {
                    var ChequeBookModel = service.SelectChequeBook(new ChequeBook() {RecordStatus = "!D" }, null);

                    for (int i = 0; i < ChequeBookModel.Length; i++)
                    {
                        if (model.Code == ChequeBookModel[i].Code)
                        {
                            ModelState.AddModelError("", "!เกิดข้อผิดพลาด เลขที่สมุดเช็คซ้ำ");
                            return View(model);
                        }
                    }

                    model.ChequeBookStatus = "1";
                    service.UpdateChequeBook(model, WebSecurity.CurrentUserId);
                    return RedirectToAction("Details", new { id = id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new ChequeBookService.ChequeBookServiceClient();
                service.DeleteChequeBook(id, WebSecurity.CurrentUserId);

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new ChequeBookService.ChequeBookServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeleteChequeBook(delete, WebSecurity.CurrentUserId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        #endregion

        #region Cheque

        public ActionResult Details(int id)
        {
            var service = new ChequeBookService.ChequeBookServiceClient();
            var model = service.SelectChequeBook(new Models.FIM.ChequeBook { ChequeBookId = id }, new[] {"MasBankAccount"}).FirstOrDefault();

            return View(model);
        }

        public ActionResult CreateCheque(int ChequeBookId)
        {
            var service = new ChequeBookService.ChequeBookServiceClient();
            var modelCheque = service.SelectChequeBook(new Models.FIM.ChequeBook { ChequeBookId = ChequeBookId, RecordStatus = "!D" }, null).FirstOrDefault();
            var model = new Cheque();
            model.ChequeBookId = modelCheque.ChequeBookId;

            return PartialView(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateCheque(Cheque model)
        {
            try
            {
                var service = new ChequeBookService.ChequeBookServiceClient();
                var countChequeNo = service.SelectCheque(new Cheque() { ChequeNo = model.ChequeNo }, null).Length;

                if (countChequeNo > 0)
                {
                    ModelState.AddModelError("", "!เกิดข้อผิดพลาด เลขที่เช็คซ้ำ");
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        model.ChequeStatus = "1";
                        var id = service.InsertCheque(model, WebSecurity.CurrentUserId);

                        return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                    }
                }
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }

            return Json(new { result = false, message = "!เกิดข้อผิดพลาด เลขที่เช็คซ้ำ", alertClass = "" });
        }

        public ActionResult EditCheque(int id)
        {
            var service = new ChequeBookService.ChequeBookServiceClient();
            var model = service.SelectCheque(new Models.FIM.Cheque { ChequeId = id }, null).FirstOrDefault();
            //ViewBag.OldChequeNo = model.ChequeNo;

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditCheque(int id, Cheque model)
        {
            var service = new ChequeBookService.ChequeBookServiceClient();
            var oldModel = service.SelectCheque(new Cheque() { ChequeId = model.ChequeId, RecordStatus = "!D" }, null).FirstOrDefault();
            var oldChequeNo = oldModel.ChequeNo;
            var oldCBookId = oldModel.ChequeBookId;
            
                      
            try
            {
                if (oldChequeNo == model.ChequeNo)
                {
                    if (ModelState.IsValid)
                    {
                        model.ChequeStatus = "1";
                        service.UpdateCheque(model, WebSecurity.CurrentUserId);

                        return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                    }
                }
                else
                {
                    var ChequeModel = service.SelectCheque(new Cheque() { ChequeBookId = model.ChequeBookId, RecordStatus = "!D" }, null);
                    
                    for (int i = 0; i < ChequeModel.Length; i++ )
                    {
                        if (model.ChequeNo == ChequeModel[i].ChequeNo)
                        {
                            return Json(new { result = false, message = "!เกิดข้อผิดพลาด รหัสธนาคารซ้ำ", alertClass = "" });
                        }
                    }

                    model.ChequeStatus = "1";
                    service.UpdateCheque(model, WebSecurity.CurrentUserId);

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }

            return Json(new { result = false, message = "!เกิดข้อผิดพลาด เลขที่เช็คซ้ำ", alertClass = "" });
        }

        public ActionResult DeleteCheque(int id)
        {
            try
            {
                var service = new ChequeBookService.ChequeBookServiceClient();
                var ChequeId = service.SelectCheque(new Models.FIM.Cheque { ChequeId = id }, null).FirstOrDefault().ChequeBookId;
                service.DeleteCheque(id, WebSecurity.CurrentUserId);

                return RedirectToAction("Details", new { id = ChequeId });
            }
            catch
            {
                var service = new ChequeBookService.ChequeBookServiceClient();
                return View("Details", new { id = service.SelectCheque(new Models.FIM.Cheque { ChequeId = id }, null).FirstOrDefault().ChequeBookId });
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAllCheque(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new ChequeBookService.ChequeBookServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeleteCheque(delete, WebSecurity.CurrentUserId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        public ActionResult DetailsCheque(int id)
        {
            var service = new ChequeBookService.ChequeBookServiceClient();
            var model = service.SelectCheque(new Models.FIM.Cheque { ChequeId = id },null).FirstOrDefault();

            return View(model);
        }


        #endregion
    }
}
